#!/usr/bin/python
# -*- coding: utf-8 -*-
import os, sys, re
import codecs
import time
from lxml import etree

PATH_ENCODING = "utf-8"

def getSourcelang(fn):
    tree = etree.parse(fn.encode(PATH_ENCODING))
    etree.strip_tags(tree, "ut")
    etree.strip_tags(tree, "df")  
    return tree.xpath("//@SourceLanguage")[0]

def getTargetlang(fn):
    tree = etree.parse(fn.encode(PATH_ENCODING))
    etree.strip_tags(tree, "ut")
    etree.strip_tags(tree, "df")
    return tree.xpath("//@TargetLanguage")[0]
    
def getFilename(dir):
    for dirname, dirnames, filenames in os.walk(dir):
        for fn in filenames:           
            if fn.endswith(".ttx") or fn.endswith(".TTX"):
                return os.path.join(dirname,fn).encode(PATH_ENCODING)
        
def ttxRead(fn, stripTag):
    if fn.endswith(".ttx") or fn.endswith(".TTX"):
        pos = 0
        sourceList = []
        targetList = []
        fileList = []
        tree = etree.parse(fn.encode(PATH_ENCODING))
        etree.strip_tags(tree,"ut")
        etree.strip_tags(tree,"df")           
        srclang = tree.xpath("//@SourceLanguage")[0]
        tarlang = tree.xpath("//@TargetLanguage")[0]
        tu = tree.findall("//Tu")     
        if tu:        
            for tuv in tu:
                pos = pos + 1
                source = tuv.findtext("Tuv[@Lang='%s']" % srclang).strip()
                if tuv.findtext("Tuv[@Lang='%s']" % tarlang):
                    target = tuv.findtext("Tuv[@Lang='%s']" % tarlang).strip()
                else:
                    target = tuv.findtext("Tuv[@Lang='%s'][0]" % srclang).strip()
                if stripTag == True:
                    source = source.replace("<:hs>","")
                    source = re.sub("<.*?>", "", source) 
                    target = target.replace("<:hs>","")
                    target = re.sub("<.*?>", "", target)          
                sourceList.append(source)
                targetList.append(target)
                fileList.append(fn)
        else:         
            for lines in tree.findtext("Body/Raw").split("\n"):
                source = lines.strip()
                if stripTag == True:
                    source = source.replace("<:hs>","")
                    source = re.sub("<.*?>", "", source)                    
                sourceList.append(source)
                targetList.append("")
                fileList.append(fn)
        result = [sourceList, targetList, fileList] 
        return result

def ttxDirRead(dir, stripTag):
    pos = 0
    sourceList = []
    targetList = []
    fileList = []
    for dirname, dirnames, filenames in os.walk(dir):
        for fn in filenames:           
            if fn.endswith(".ttx") or fn.endswith(".TTX"):
                tree = etree.parse(os.path.join(dirname,fn).encode(PATH_ENCODING))
                etree.strip_tags(tree,"ut")
                etree.strip_tags(tree,"df")
                srclang = tree.xpath("//@SourceLanguage")[0]
                tarlang = tree.xpath("//@TargetLanguage")[0]
                tu = tree.findall("//Tu")   
                if tu:  
                    for tuv in tu:
                         pos = pos + 1
                         source = tuv.findtext("Tuv[@Lang='%s']" % srclang).strip()
                         if tuv.findtext("Tuv[@Lang='%s']" % tarlang):
                             target = tuv.findtext("Tuv[@Lang='%s']" % tarlang).strip()
                         else:
                             target = tuv.findtext("Tuv[@Lang='%s'][0]" % srclang).strip()                                          
                         if stripTag == True:
                             source = source.replace("<:hs>","")
                             source = re.sub("<.*?>", "", source) 
                             target = target.replace("<:hs>","")
                             target = re.sub("<.*?>", "", target)               
                         sourceList.append(source)
                         targetList.append(target)
                         fileList.append(os.path.join(dirname,fn))
                else:         
                    for lines in tree.findtext("Body/Raw").split("\n"):
                        source = lines.strip()
                        if stripTag == True:
                            source = source.replace("<:hs>","")
                            source = re.sub("<.*?>", "", source)                    
                        sourceList.append(source)
                        targetList.append("")
                        fileList.append(fn)                         
    result = [sourceList, targetList, fileList]
    return result

def txtExport(fn, tab, stripTag):
    encoding = "utf-16le"
    if tab.endswith(".ttx") or tab.endswith(".TTX"):
        data = ttxRead(tab, stripTag)
    else:
        data = ttxDirRead(tab, stripTag)      
    if data: 
        txt = codecs.open(fn, "w", encoding)
        txt.write(u"\uFEFF")    
        for pos in range(0,len(data[0])):
            source = data[0][pos]
            target = data[1][pos]
            file = data[2][pos]
            txt.write("%s\t%s\t%s\n" % (source, target, file))                     
        txt.close()
            
def tmxExport(fn, tab, stripTag, product, version):
    encoding = "utf-8"
    date = time.strftime("%Y%m%dT%H%M%S", time.localtime()) + "Z"
    srclang = "EN-US"
    tarlang = "EN-US"
    if tab.endswith(".ttx") or tab.endswith(".TTX"):
        data = ttxRead(tab, stripTag)
        srclang = getSourcelang(tab)
        tarlang = getTargetlang(tab)
    else:
        data = ttxDirRead(tab, stripTag)
        oneFile = getFilename(tab)
        srclang = getSourcelang(oneFile)
        tarlang = getTargetlang(oneFile)           
    if data: 
        tmx = codecs.open(fn, "w", encoding)
        tmx.write(u"\uFEFF")    
        tmx.write("<?xml version=\"1.0\" ?>\n")
        tmx.write("<!DOCTYPE tmx SYSTEM \"tmx14.dtd\">\n")
        tmx.write("<tmx version=\"1.4\">\n")
        tmx.write("<header\n")
        tmx.write("\t creationtool=\"%s\"\n" % product)
        tmx.write("\t creationtoolversion=\"%s\"\n" % version)
        tmx.write("\t segtype=\"sentence\"\n")
        tmx.write("\t adminlang=\"%s\"\n" % srclang)
        tmx.write("\t srclang=\"%s\"\n" % srclang)
        tmx.write("\t datatype=\"rtf\"\n")
        tmx.write("\t o-tmf=\"%s\">\n" % product)
        tmx.write("</header>\n")
        tmx.write("<body>\n")       
        for pos in range(0,len(data[0])):
            source = data[0][pos]
            target = data[1][pos]           
            tmx.write("<tu tuid=\"%s\" srclang=\"%s\">\n" % (str(pos), srclang))
            tmx.write("<tuv xml:lang=\"%s\">\n" % srclang)
            tmx.write("<seg>%s</seg>\n" % source)
            tmx.write("</tuv>\n")
            tmx.write("<tuv xml:lang=\"%s\" creationdate=\"%s\" creationid=\"%s\" lastusagedate=\"%s\" changedate=\"%s\" changeid=\"%s\" usagecount=\"0\">\n"
                      % (tarlang, date, product, date, date, product))                     
            tmx.write("<seg>%s</seg>\n" % target) 
            tmx.write("</tuv>\n")
            tmx.write("</tu>\n")                     
        tmx.write("</body>\n")
        tmx.write("</tmx>\n")    
        tmx.close()
            